<template>
  <div>测试内容</div>
  <div class="patrol-record-stats">
    <div class="search-bar">
      <el-input v-model="searchName" placeholder="巡查人" style="width: 200px; margin-right: 10px;" />
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button @click="handleReset">重置</el-button>
    </div>
    <div style="margin: 10px 0;">
      查询结果：共计 {{ total }} 条数据
    </div>
    <el-table :data="tableData" :loading="loading" style="width: 100%;">
      <el-table-column prop="realName" label="巡查人" />
      <el-table-column prop="patrolCount" label="巡查次数">
        <template #default="scope">
          <el-link type="primary" @click="goDetail(scope.row)">{{ scope.row.patrolCount }}</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next, jumper"
      style="margin-top: 20px; text-align: right;"
      @current-change="fetchData"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { fetchPatrolRecordStats } from '@/api/patrolRecord'

const searchName = ref('')
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const tableData = ref([])
const loading = ref(false)
const router = useRouter()

const fetchData = async () => {
  loading.value = true
  try {
    const res = await fetchPatrolRecordStats({
      realName: searchName.value,
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })
    tableData.value = res.data?.records || []
    total.value = res.data?.total || 0
    console.log('tableData', tableData.value)
  } finally {
    loading.value = false
  }
}

function handleSearch() {
  currentPage.value = 1
  fetchData()
}
function handleReset() {
  searchName.value = ''
  currentPage.value = 1
  fetchData()
}
function goDetail(row) {
  router.push({
    path: '/main/patrol/patrol-record-detail',
    query: { inspectorId: row.id, realName: row.realName }
  })
}

fetchData()
</script>

<style scoped>
.patrol-record-stats {
  background: #fff;
  padding: 24px;
  border-radius: 8px;
}
.search-bar {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}
</style>
